package com.demo.web.response;


import com.demo.enums.ResponseStatus;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 普通的响应值封装
 * 
 *
 * @param <T>
 */
@Data
@ApiModel("响应实体")
public class Response<T> {

    @ApiModelProperty(value = "返回标志")
    private int code;
    @ApiModelProperty(value = "描述")
    private String msg;
    @ApiModelProperty(value = "数据")
    private T data;

    protected Response() {
    }

    protected Response(int status) {
        this.code = status;
    }

    protected Response(int status, String message) {
        this.code = status;
        this.msg = message;
    }

    protected Response(int status, T data) {
        this.code = status;
        this.data = data;
    }

    protected Response(int status, String message, T data) {
        this.code = status;
        this.data = data;
        this.msg = message;
    }

    public static <T> Response<T> ok() {
        return ok(null);
    }

    public static <T> Response<T> ok(T data) {
        return new Response<>(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), data);
    }


    public static <T> Response<T> error(ResponseStatus status, String message) {
        return new Response<>(status.getCode(), message, null);
    }

    public static <T> Response<T> error(ResponseStatus status) {
        return error(status, status.getMessage());
    }

}
